package br.edu.ufcg.dsc.actions.loan;

import java.text.DecimalFormat;
import java.util.List;

import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import com.opensymphony.xwork2.Preparable;

import br.edu.ufcg.dsc.actions.BaseLoggedAction;
import br.edu.ufcg.dsc.domain.Loan;
import br.edu.ufcg.dsc.services.LoanService;
import br.edu.ufcg.dsc.services.UserService;

@Result(name="success", type=ServletDispatcherResult.class, value="/jsp/loan/showLoans.jsp")
public class ChooseUser extends BaseLoggedAction implements Preparable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String user;
	
	private List<Loan> loans;
	
	@Override
	protected String executeIfLogged() throws Exception {		
		return SUCCESS;
	}

	public List<Loan> getLoans() {
		return loans;
	}

	public void setLoans(List<Loan> loans) {
		this.loans = loans;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getTotalFine() {
		DecimalFormat formatter = new DecimalFormat("0.00");

		return formatter.format(UserService.find(user).calculateFine());
	}

	public void prepare() throws Exception {
		setLoans(LoanService.findByUser(user));		
	}


}
